package com.longcai.service.impl.nearby;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.bean.ApiResult;
import com.longcai.entity.nearby.ClyjNearbyPackage;
import com.longcai.entity.nearby.ClyjNearbyPackageGoods;
import com.longcai.mapper.nearby.ClyjNearbyPackageMapper;
import com.longcai.service.nearby.ClyjNearbyPackageService;
import com.longcai.util.StringUtils;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/****
 * 获取店铺团购页
 * @author kh
 *
 */
@Service
public class ClyjNearbyPackageServiceImpl extends ServiceImpl<ClyjNearbyPackageMapper, ClyjNearbyPackage> implements ClyjNearbyPackageService {

	@Autowired
	private ClyjNearbyPackageMapper  clyjNearbyPackageMapper;
	@Override
	public ApiResult<?> getNearbyPackageList(String shopsid) {
		QueryWrapper<ClyjNearbyPackage> queryWrapper= new QueryWrapper<ClyjNearbyPackage>();
		queryWrapper.eq("shop_id", shopsid);
		List<ClyjNearbyPackage> list=clyjNearbyPackageMapper.selectList(queryWrapper);
		for (ClyjNearbyPackage clyjNearbyPackage : list) {
			String picPath=clyjNearbyPackage.getPackageBanner();//取团购图第一张作为主图
			if(!StringUtils.isEmpty(picPath)){
				clyjNearbyPackage.setPackageBanner(picPath.substring(0));
			}
			StringBuffer sb= new StringBuffer();
			List<ClyjNearbyPackageGoods> clyjNearbyPackageGoodsList=clyjNearbyPackage.getClyjNearbyPackageGoodsList();
			if(clyjNearbyPackageGoodsList==null||clyjNearbyPackageGoodsList.size()<1){
				return ApiResult.fail("无对应套餐详情");
			}
			for (int i = 0; i < clyjNearbyPackageGoodsList.size(); i++) {
				if (i == clyjNearbyPackageGoodsList.size() - 1) {  
				    sb.append(clyjNearbyPackageGoodsList.get(i).getGroupName());  
				    } else {  
				    	sb.append(clyjNearbyPackageGoodsList.get(i).getGroupName());
						sb.append(","); 
				    } 
			}
			clyjNearbyPackage.setPackageDepict(sb.toString());//商品名称下的描述（取自团购详情的           团购名（套餐详情名称）20190506确定）
			list.add(clyjNearbyPackage);
		}
		return ApiResult.success(list);
	}
	
	@Override
	public ApiResult<?> getNearbyPackage(String shopsid, String packageId) {
		QueryWrapper<ClyjNearbyPackage> queryWrapper= new QueryWrapper<ClyjNearbyPackage>();
		queryWrapper.eq("shop_id", shopsid);
		queryWrapper.eq("package_id", packageId);
		ClyjNearbyPackage clyjNearbyPackage=clyjNearbyPackageMapper.selectOne(queryWrapper);
		return ApiResult.success(clyjNearbyPackage);
	}

}
